﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFIDA.U9.SM.Ship;
using UFIDA.U9.PM.PO;
using UFSoft.UBF.PL;
using UFIDA.U9.PM.Rcv;
using UFIDA.U9.SM.RMA;
using UFIDA.U9.SM.SO;
using UFIDA.U9.CBO.SCM.Customer;
using UFIDA.U9.PR.PurchaseRequest;
using UFIDA.U9.CBO.FI.Enums;
using UFSoft.UBF.Util.DataAccess;
using UFSoft.UBF.PL.Engine;


namespace UFIDA.U9.Cust.LS.PlugBE
{
    /// <summary>
    /// 爱发科
    /// 请款单
    /// 提交时,清空私有扩展字段 9
    /// </summary>
    class AFKREExtend : IEventSubscriber
    {
        //配置XX.sub.xml文件放置到portal/bin下，dll文件放置到portal/ApplicationServer/Libs下
        public void Notify(params object[] args)
        {
            if (args != null && args.Length != 0 && args[0] is EntityEvent)
            {
                BusinessEntity.EntityKey key = ((EntityEvent)args[0]).EntityKey;
                 UFIDA.U9.AP.Payment.PayReqFundHead be = key.GetEntity() as UFIDA.U9.AP.Payment.PayReqFundHead;
                if (be == null)
                    return;
                if (be.SysState == ObjectState.Inserted)
                {
                    //新增/复制
                    //初始版本
                    be.DescFlexField.PrivateDescSeg9 = "";
                }

                else  if ((be.DocStatus == InDirectHandleBillDocStatusEnum.InApprove && be.OriginalData.DocStatus == InDirectHandleBillDocStatusEnum.Open))
                {
                    //清空私有字段

                    DateTime now = DateTime.Now;
                    string formatted = now.ToString("yyyy-MM-dd HH:mm:ss");
                    //String sql = $"Update AP_PayReqBillHead set DescFlexField_PrivateDescSeg4='',DescFlexField_PrivateDescSeg6='',DescFlexField_PrivateDescSeg9='{formatted}' WHERE docno='{be.DocNo}'";
                    //    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null);
                    be.DescFlexField.PrivateDescSeg4 = "";
                      be.DescFlexField.PrivateDescSeg8 = formatted;

                    //版本+1
                    if (string.IsNullOrEmpty(be.DescFlexField.PrivateDescSeg9))
                    {
                        //版本初始2
                        be.DescFlexField.PrivateDescSeg9 = "1";
                    }
                    else
                    {
                        //版本+1
                        int ver = Int32.Parse(be.DescFlexField.PrivateDescSeg9);
                        be.DescFlexField.PrivateDescSeg9 = (ver + 1).ToString();
                    }
                }
            }
        }
    }
}
